﻿// OperatorListCtrl.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "OperatorListCtrl.h"


// COperatorListCtrl

IMPLEMENT_DYNAMIC(COperatorListCtrl, CListCtrl)

COperatorListCtrl::COperatorListCtrl()
{

}

COperatorListCtrl::~COperatorListCtrl()
{
}


BEGIN_MESSAGE_MAP(COperatorListCtrl, CListCtrl)
	ON_WM_CREATE()
	ON_WM_SIZE()
END_MESSAGE_MAP()



// COperatorListCtrl 消息处理程序




int COperatorListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CListCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	Init2();

	return 0;
}


void COperatorListCtrl::OnSize(UINT nType, int cx, int cy)
{
	CListCtrl::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
}

void COperatorListCtrl::Init2()
{
	ModifyStyle(0, LVS_REPORT | LVS_SINGLESEL);
	SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_AUTOSIZECOLUMNS| LVS_EX_CHECKBOXES);

	LV_COLUMN   lvc;
	lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;
	lvc.fmt = LVCFMT_LEFT;

	lvc.iSubItem = 0;
	lvc.pszText = _T("算子名称");
	lvc.cx = 200;
	InsertColumn(1, &lvc);

	//InsertColumn(0, "算子名称", LVCFMT_LEFT, 150);
	InsertColumn(1, "所属模块", LVCFMT_LEFT, 150);
	InsertColumn(2, "开发者", LVCFMT_LEFT, 150);
	InsertColumn(3, "算子说明", LVCFMT_LEFT, 300);
}

void COperatorListCtrl::Populate(ck::Module* pModule)
{
	this->DeleteAllItems();

	ck::Operator* pOperator = NULL;
	z_uint32 nCount = pModule->GetOperatorCount();
	for (z_uint32 i = 0; i < nCount; i++)
	{
		pOperator = pModule->GetOperator(i);
		Populate(pOperator);
	}
}

/**
 * 将Opertor的信息填写到Operator列表上
 */
void COperatorListCtrl::Populate(ck::Operator* pOperator, BOOL checked/*=FALSE*/)
{
	ck::Module* pModule = pOperator->GetModule();

	int nItem = InsertItem(GetItemCount(), pOperator->GetName());	
	SetItemText(nItem, 1, pModule->GetName());
	SetItemText(nItem, 2, pModule->GetDeveloper());
	SetItemText(nItem, 3, pOperator->GetDescription());

	z_uint32 code = pOperator->GetCode();
	SetItemData(nItem, (DWORD)code);
	SetCheck(nItem, checked);
}

void COperatorListCtrl::Populate(ck::Module* pModule, z_uint32 nCheckItemCode, BOOL bShowAll)
{
	this->DeleteAllItems();

	ck::ElementRepository* pElementRepository = ck::ElementRepository::GetInstance();

	ck::Operator* pOperator = NULL;
	z_uint32 nCount = pModule->GetOperatorCount();
	for (z_uint32 i = 0; i < nCount; i++)
	{
		pOperator = pModule->GetOperator(i);
		if (pElementRepository->IsOperatorMounted(nCheckItemCode, pOperator->GetCode()))
		{
			Populate(pOperator, TRUE);
		}
		else
		{
			if (bShowAll)
			{
				Populate(pOperator, FALSE);
			}
			
		}
			
	}
}

void COperatorListCtrl::Populate(z_uint32 nCheckItemCode, BOOL isShowAll/*=FALSE*/)
{
	this->DeleteAllItems();

	ck::Module* pModule = NULL;
	ck::ModuleManager* pModuleManager = ck::ModuleManager::GetInstance();

	//遍历每一个mod_模块
	z_uint32 nCount = pModuleManager->GetCount();
	for (z_uint32 i = 0; i < nCount; i++)
	{
		pModule = pModuleManager->GetModule(i);
		if (pModule)
		{
			ck::ElementRepository* pElementRepository = ck::ElementRepository::GetInstance();
			
			//遍历mod_中的Operator，判断当前mod_中的Operator是否挂载到mod_实例上。
			ck::Operator* pOperator = NULL;
			z_uint32 nCount = pModule->GetOperatorCount();
			for (z_uint32 i = 0; i < nCount; i++)
			{
				pOperator = pModule->GetOperator(i);
				//判断当前mod_中的Operator是否挂载到mod_实例上。
				if (pElementRepository->IsOperatorMounted(nCheckItemCode, pOperator->GetCode()))
				{
					//如果已经挂载，则将其添加到Opertor列表中
					Populate(pOperator, TRUE);
				}
				else
				{
					if (isShowAll)
					{
						Populate(pOperator, FALSE);
					}
				}

			}
		}
	}
}